package dao.mapMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import modelInterfaces.AbstractModelFactory;
import modelInterfaces.IEvent;
import modelInterfaces.IEventType;
import modelInterfaces.ISubject;
import modelInterfaces.IValue;
import dao.DAOException;
import dao.DAOFactory;
import dao.IEventDAO;
import dao.IEventTypeDAO;
import dao.IObjectDAO;
import dao.ISubjectDAO;
import dao.ITestConnection;
import dao.IValueDAO;
import dao.mysql.MySqlDBConnection;
import dao.mysql.TestConnectionMySql;

/**
 *
 * @author salvador
 */
public class MapDAOFactory extends DAOFactory {

	public static Map<String,List<IEvent>> mapEvents;

    public MapDAOFactory()
    {
    	
    }
	
	public IEventDAO getEventDAO()
    {
        return new EventMapMysqlDAO();
    }

    public IEventTypeDAO getEventTypeDAO()
    {
        return new EventTypeMapMySqlDAO();
    }

	@Override
	public ISubjectDAO getUserDAO() {
		return new SubjectMapMySqlDAO();
	}

	@Override
	public IValueDAO getValueDAO() {
		return new ValueMapMySqlDAO();
	}

	@Override
	public IObjectDAO getItemDAO() {
		return new ObjectMapMySqlDAO();
	}
	
	public static void updateMapEvents(int eventType) throws DAOException {
        ResultSet rs = MySqlDBConnection.getInstance().executeQuery("SELECT DISTINCT(e.id), e.subject, e.object, e.time, v.value "
                + "FROM `event` e, `value` v  WHERE e.id = v.event AND `eventType` = "+eventType+" ORDER BY e.subject");
        List<IEvent> eventList = null;
        mapEvents = new HashMap<String, List<IEvent>>();
        try
        {
            while (rs.next())
            {
            	String user = rs.getString("subject");
                if(!mapEvents.containsKey(user)){
                    eventList = new ArrayList<IEvent>();
                    mapEvents.put(user, eventList);
                }
                IEvent event = AbstractModelFactory.getModelFactory().getEvent();
                IEventType type = AbstractModelFactory.getModelFactory().getEventType();
                type.setId(eventType);
                ISubject subject = AbstractModelFactory.getModelFactory().getUser();
                subject.setId(Integer.parseInt(user));
                event.setEventType(type);
                event.setId(rs.getInt("id"));
                event.setSubject(user);
                event.setObject(rs.getString("object"));
                event.setTime(rs.getString("time"));
                List<IValue> values = new ArrayList<IValue>();
                IValue value = AbstractModelFactory.getModelFactory().getValue();
                value.setValue(rs.getString("value"));
                values.add(value);
                event.setValues(values);
                eventList.add(event);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
    }

	@Override
	public ITestConnection getTestConnection() {
		return new TestConnectionMySql();
	}


}
